package net.gxf.common.etf;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

import net.gxf.common.util.ResourceLoader;

/**
 * load ETF data：
 * 
 * 1：implement to load from network , must set the URL
 * 
 * 2：implement to load from native fileSystem by file path
 * 
 * @author: maybe
 * 
 */
public class ETFLoader {

	/**
	 * 
	 * file's URL
	 * 
	 * @param url
	 * @return
	 */
	public static ETF load(String url) {
		
		ETF etf = new ETF();
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResourceAsStream(url)));
			String line = null;
			while ((line = reader.readLine()) != null) {
				if (line.startsWith("[ETF") || line.startsWith("TAGTAG"))
					continue;
				if (line.contains("=")) {
					dealETFHead(etf, line);
				}
				if (line.contains("|")) {// 读取ETF股票
					dealETFStock(etf, line);
				}
				if (line.equalsIgnoreCase("ENDENDEND"))
					break;
			}
			reader.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return etf;
	}

	/** 读取ETF头部信息 */
	private static void dealETFHead(ETF etf, String line) {
		String[] strings = line.split("=");
		if (strings[0].equalsIgnoreCase("Fundid1") || strings[0].equalsIgnoreCase("FundID")) {
			etf.setFundid1(strings[1]);
		} else if (strings[0].equalsIgnoreCase("CreationRedemptionUnit")) {
			etf.setCreationRedemptionUnit(Integer.valueOf(strings[1]));
		} else if (strings[0].equalsIgnoreCase("MaxCashRatio")) {
			etf.setMaxCashRatio(Float.valueOf(strings[1]));
		} else if (strings[0].equalsIgnoreCase("Recordnum")) {
			etf.setRecordnum(Integer.valueOf(strings[1]));
		} else if (strings[0].equalsIgnoreCase("EstimateCashComponent")) {
			etf.setEstimateCashComponent(Float.valueOf(strings[1]));
		} else if (strings[0].equalsIgnoreCase("CashComponent")) {
			etf.setCashComponent(Float.valueOf(strings[1]));
		} else if (strings[0].equalsIgnoreCase("NAVperCU")) {
			etf.setNAVperCU(Float.valueOf(strings[1]));
		} else if (strings[0].equalsIgnoreCase("NAV")) {
			etf.setNAV(Float.valueOf(strings[1]));
		}
	}

	/** 读取ETF股票信息 */
	private static void dealETFStock(ETF etf, String line) {
		String[] strings = line.split("[|]");
		StockETF stockeEtf = new StockETF();
		stockeEtf.setCode(strings[0].trim());
		stockeEtf.setName(strings[1].trim());

		if (strings[2].trim().length() != 0)// 必须用现金的股票，不设置数量，默认为0
			stockeEtf.setNumber(Integer.valueOf(strings[2].trim()));

		stockeEtf.setFlag(Byte.valueOf(strings[3]));

		if (stockeEtf.getFlag() == 2) {
			stockeEtf.setRate(0);
			stockeEtf.setReplace(Float.valueOf(strings[5]));
		}
		etf.addStock(stockeEtf);
	}



	// Test
	/*
	 * public static void main(String[] args) { Loader_ETF loader_ETF = new
	 * Loader_ETF(); // ETF etf_SH = // loader_ETF.loadFromNet_SH(
	 * "http://www.sse.com.cn/sseportal/webapp/datapresent/ETF2download?ETF_TYPE=1"
	 * ); ETF etf_SH = loader_ETF .loadFromNet(
	 * "http://www.szse.cn/szseWeb/common/szse/files/text/etfDown/159901etf20121018.txt"
	 * ); System.out.println("llllllll");
	 * 
	 * }
	 */
}
